//
//  PostedTypeTools.m
//  Village
//
//  Created by 程虎 on 2017/9/19.
//  Copyright © 2017年 程虎. All rights reserved.
//

#import "PostedTools.h"

@implementation PostedTools

+ (void)GetPostedtypeSuccess:(void (^)(NSArray *valueAry))success failure:(void (^)(NSString *error))failure;
{
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        NSMutableDictionary *bean = [NSMutableDictionary dictionary];
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        [bean setValue:dict forKey:@"entitybean"];
        [NetRequestClass Post:PostedTypeURL Parameters:bean success:^(id result) {
            if (success) {
                success([PostedType mj_objectArrayWithKeyValuesArray:result[@"data"]]);
            }
        } failure:^(id valueCode, id error) {
            if (failure) {
                failure(error);
            }
        }];
    });
}

/**
 发布文章
 */
+ (void)GetPostedNewsWithTitle:(NSString *)titles WithContents:(NSString *)contents WithAddress:(NSString *)addressStr WithType:(NSString *)types Photo:(id )imgs Success:(void (^)(NSArray *valueAry))success failure:(void (^)(NSString *error))failure
{
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        NSMutableDictionary *bean = [NSMutableDictionary dictionary];
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        [bean setValue:dict forKey:@"entitybean"];
        [dict setValue:titles forKey:@"title"];
        [dict setValue:contents forKey:@"content"];
        
        NSDate *date = [Tools getInternetDate];
        
        NSString *string = [Tools stringFromDate:date];
        LSLog(@"msg -----  %@",string);
        NSString *str =  [string substringToIndex:20];//截取掉下标7之后的字符串
        
        
        [dict setValue:str forKey:@"publishtime"];
        [dict setValue:[addressStr substringFromIndex:3] forKey:@"aread"];
        [dict setValue:[Globle getInstance].account.areaid.length > 0 ? [Globle getInstance].account.areaid : ([Globle getInstance].areaid.length > 0 ? [Globle getInstance].areaid : @"") forKey:@"areadid"];
        [dict setValue:[Globle getInstance].account.userid forKey:@"publishid"];
        [dict setValue:types forKey:@"code3"];
        [dict setValue:imgs forKey:@"upimg"];
        [dict setValue:[Globle getInstance].account.account forKey:@"publishuser"];
        [NetRequestClass Post:PostedNewsURL Parameters:bean success:^(id result) {
            if (success) {
                success([PostedType mj_objectArrayWithKeyValuesArray:result[@"data"]]);
            }
        } failure:^(id valueCode, id error) {
            if (failure) {
                failure(error);
            }
        }];
    });
}



/**
 上传图片
 */
+ (void)UploadImgsWithIMGArys:(NSArray *)arrays Success:(void (^)(NSArray *valueAry))success failure:(void (^)(NSString *error))failure
{
    __block BOOL isShow = NO;
    __block BOOL isf = YES;
    __block NSInteger index = 0;
    NSMutableArray *photoAry = [NSMutableArray array];
    NSString * fileName = [NSString stringWithFormat:@"%@.png",[Tools getCurrentDate]];
    for (int i = 0; i < arrays.count; i++) {
       
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            UIImage *images = arrays[i];
            [[AFNetWorkService2 getAFNetWorkService2] leapUploadUrl:LeapUrl namePath:@"default" image:[Tools imageCompressWithSimple:images scale:1] fileName:fileName mimeType:@"application/octet-stream" progressBlock:^(float progress) {
                NSLog(@"progress==%f",progress);
            } completeBlock:^(id result, NSString *downloadUrl) {
                index += 1;
                if (downloadUrl.length > 0 ) {
                    [photoAry addObject:result];
                    isf = YES;
                }else{
                    isf = NO;
                }
                if (index == arrays.count) {
                    if (success) {
                        success(photoAry);
                        NSLog(@" 上传成功了一次");
                    }
                } else {
                    if (failure) {
                        if (isf == NO){//判断是否成功
                            if (!isShow) {
                                isShow = YES;
                                failure(@"图片上传失败");
                            }
                        }
                        
                    }
                }
             
            }];
        });
    }
    
}
@end
